from django.db import models
from user.models import UserModel
from goods.models import ProductModel
from coupon.models import CouponModel,PointsModel
# Create your models here.


# 订单详情表
class OrderDetailsModel(models.Model):
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="当前用户ID")
    drug = models.ForeignKey(ProductModel, on_delete=models.CASCADE, verbose_name="当前商品ID")
    quantity = models.IntegerField(verbose_name="购买数量")
    unit_price = models.DecimalField(max_digits=9, decimal_places=2, verbose_name="单价")
    total_price = models.DecimalField(max_digits=9, decimal_places=2, verbose_name="小计")
    order_status = models.IntegerField(choices=(
        (1, '未支付'),
        (2, '已支付'),
        (3, '已取消'),
        (4, '已超时'),
    ), verbose_name="订单状态")

    def __str__(self):
        return f"订单详情编号: {self.id}, 商品名称: {self.drug.name}, 数量: {self.quantity}"
    class Meta:
        db_table = "order_details"
        verbose_name = "订单详情表"
        verbose_name_plural = verbose_name
# 订单表
class OrderModel(models.Model):
    PAYMENT_TYPES = (
        (1, '微信'),
        (2, '支付宝'),
        # 可以继续添加其他支付类型
    )

    ORDER_STATUS_CHOICES = (
        (1, '未支付'),
        (2, '已支付'),
        (3, '已取消'),
        (4, '已超时'),
    )

    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name="当前用户ID")
    pay_type = models.IntegerField(choices=PAYMENT_TYPES, verbose_name="支付类型")
    transaction_no = models.CharField(max_length=50, verbose_name="支付流水号", null=True)
    total_count = models.IntegerField(verbose_name="总件数")
    total_price = models.DecimalField(max_digits=9, decimal_places=2, verbose_name="总价")
    order_no = models.CharField(max_length=50, unique=True, verbose_name="订单号")
    order_status = models.IntegerField(choices=ORDER_STATUS_CHOICES, default=1, verbose_name="订单状态")
    ordered_at = models.DateField(null=True, blank=True, verbose_name="下单时间")
    shipped_at = models.DateField(null=True, blank=True, verbose_name="发货时间")
    delivered_at = models.DateField(null=True, blank=True, verbose_name="送达时间")
    cancelled_at = models.DateField(null=True, blank=True, verbose_name="取消时间")
    orderdetail = models.OneToOneField(OrderDetailsModel, on_delete=models.CASCADE, null=True, blank=True,
                                       verbose_name="订单详情")
    coupon = models.OneToOneField(CouponModel, on_delete=models.CASCADE, null=True, blank=True,
                                        verbose_name="订单优惠券")  # 一对一(优惠券表)
    points = models.OneToOneField(PointsModel, on_delete=models.CASCADE, null=True, blank=True,
                                  verbose_name="积分")  # 一对一(积分表)

    def __str__(self):
        return f"订单编号: {self.id}, 总价: {self.total_price}"

    class Meta:
        db_table = "order"
        verbose_name = "订单表"
        verbose_name_plural = verbose_name


# 评价表
class EvaluateModel(models.Model):
    order = models.ForeignKey(OrderModel, on_delete=models.CASCADE, verbose_name='订单')
    RATING_CHOICES = [
        (1, '1星'),
        (2, '2星'),
        (3, '3星'),
        (4, '4星'),
        (5, '5星'),
    ]
    rating = models.SmallIntegerField(choices=RATING_CHOICES, verbose_name='评分')
    comment = models.TextField(verbose_name='评论', help_text='对药品或服务的评价')
    created_at = models.DateTimeField(auto_now_add=True, verbose_name='评价时间')
    user = models.ForeignKey(UserModel, on_delete=models.CASCADE, verbose_name='用户')

    def __str__(self):
        return self.comment

    class Meta:
        db_table = 'evaluate'
        verbose_name = '用户评价表'
        verbose_name_plural = verbose_name
